<?
    require_once("config.php");
    
    if ($_GET['action'] == 'new') {
        Functions::PrintHeader("Create Project");
        ?>
        <script type='text/javascript'>
            function js_checkValidity(value) {
                var url = "ajax.php?action=check_project_name_validity&name=" + value;
                new Ajax.Request(url, {
                    method : 'GET',
                    onSuccess : function (transport) {
                        result = transport.responseText * 1;
                        $("name_label").innerHTML = (!result ? "Not " : '') + "Available";
                        color = "#FFDDDD";
                        if (result) color = "#DDFFDD";
                        $("name_label").style.background = color;
                        $("name").style.background = color;
                    }
                });
            }
        </script>
        <form method='POST' action='projecthandler.php'>
            <input type='hidden' name='action' value='new' />
            <table class='cgltable' align='center'>
                <tr>
                    <th colspan='2'>Create Project</th>
                </tr>
                <tr>
                    <td class='label'>Project Name</td>
                    <td>
                        <input type='text' name='name' id='name' onKeyUp='this.background = js_checkValidity(this.value)' />
                        <span id='name_label' style='font-weight:bold; border:solid 1px black; padding:3px; margin: 2px;'>&nbsp;&nbsp;.&nbsp;&nbsp;.&nbsp;&nbsp;.&nbsp;&nbsp;</span>
                    </td>
                </tr>
                <tr>
                    <td colspan='2'>
                        <input type='reset' value='Start Over' />
                        <input type='submit' value='Create Project' />
                    </td>
                </tr>
            </table>
        </form>
        <?
        Functions::PrintFooter();
    }
    else if ($_GET['action'] == 'open') {
        Functions::PrintHeader("Open Project");
        echo "<ul>";
        foreach (scandir("projects") as $project) {
            if ($project[0] == '.') continue;
            if (is_dir("projects/$project")) echo "<a href='project.php?action=work&project=$project'>$project</a>";
        }
        echo "</ul>";
        Functions::PrintFooter();
    }
    else if ($_GET['action'] == 'work') {
        Functions::PrintHeader("Editing Project " . $_GET['project']);
        ?>
        <div>
            <div style='float:left; width:400px;'>
                <form method='POST' enctype='multipart/form-data' action='ajax.php'>
                    <input type='hidden' name='action' value='addframe' />
                    <input type='hidden' name='project' value='<?=$_GET['project']?>' />
                </form>
                
                <table width='400' class='cgltable'>
                    <tr>
                        <th colspan='2'>Add A Frame</th>
                    </tr>
                    <tr>
                        <td class='label'>Select File</td>
                        <td><input type='file' name='curfile' id='curfile' /></td>
                    </tr>
                    <tr>
                        <td colspan='2'><input type='submit' onClick='addfile()' value='Add Frame' /></td>
                    </tr>
                </table>
        
                <hr width='100%' />
                <div id='statustext'></div>
                <div id='statusbar'></div>
                
                <!-- frames are added to this area -->
                <div style='width:600px;' id='frames'>
                <?
                    $project = $_GET['project'];
                    foreach (scandir("projects/$project/thumbnails") as $frame) {
                        if ($frame[0] == '.') continue;
                        echo "<img src='projects/$project/thumbnails/$frame' />";
                    }
                ?>
                </div>
            </div>
            <div style='float:left;'>
                The player goes here
            </div>
        </div>
        <hr width='100%' />
        <div id='storyboard'>
            Build Storyboard Here
        </div>
        <?
        Functions::PrintFooter();
    }
    else {
    }
?>